#pragma once

#include "Mutex.h"
#include "Handle.h"
#include "ScopedLockers.h"
#include <vector>
#include <functional>
#include <queue>
#include <boost/thread.hpp>
#include <memory>

namespace syncLib
{

    typedef std::auto_ptr<boost::thread> ThreadPtr;

    class ITask
    {
    public:
        virtual ~ITask() {}
        virtual void Run() = 0;
    };

    class ThreadPoolImpl 
    {
    public:
        explicit ThreadPoolImpl(size_t maxThreads);
        ~ThreadPoolImpl(void);

    public:

        void ScheduleTask(ITask* task);
        size_t ActiveThreadCount() const;
        size_t MaxThreadCount() const;
        bool IsEmptyTaks() const;
        bool WaitForDone(int msecs);

    private:
        void ThreadPoolFunction();
        void PopAndExecuteTask();
        bool TryStartOnMoreThread();
        void StartThread();

    private:
        Mutex mutex_;
        std::vector<boost::thread> workers_;
        std::queue<ITask*> tasks_;
        size_t maxThreadCount_;
        Handle hStopEvent_;
        Handle hNotEmptyEvent_;
    };

    typedef std::shared_ptr<ThreadPoolImpl> ThreadPoolImplPtr;


};